package service

import (
	"database/sql"
	"errors"
	"gin-epg/internal/app/entity"
	"gin-epg/internal/app/mapper"
	"gin-epg/internal/app/param"
	"time"
)

/*
新建EpgChannel信息
*/
func CreateEpgChannelAlias(epgChannelAliasParam *param.AddEpgChannelAliasParam) (err error) {
	// 先检查name在标准频道名是否存在，若不存在，直接提示 请添加频道名
	_, err = mapper.GetEpgChannelByName(epgChannelAliasParam.Name)
	if err != nil {
		return errors.New("标准频道名不存在")
	}

	// *param.AddEpgChannelAliasParam 转换为 entity.EpgChannelAlias
	isExportXmltv := "0"
	if epgChannelAliasParam.IsExportXmltv != "" {
		isExportXmltv = epgChannelAliasParam.IsExportXmltv
	}

	epgChannelAlias := &entity.EpgChannelAlias{
		Alias:         epgChannelAliasParam.Alias,
		IsExportXmltv: isExportXmltv,
		Name:          epgChannelAliasParam.Name,
		CreateTime:    entity.NullTime{sql.NullTime{Time: time.Now(), Valid: true}}, // 设置为当前时间
	}

	if err = mapper.CreateEpgChannelAlias(epgChannelAlias); err != nil {
		return err
	}
	return
}

/*
*
获取epgChannel集合
*/
func GetAllEpgChannelAlias() (epgChannelList []*entity.EpgChannelAlias, err error) {
	epgChannelList, err = mapper.GetAllEpgChannelAlias()
	if err != nil {
		return nil, err
	}
	return epgChannelList, nil
}

/*
*
根据id删除epgChannel
*/
func DeleteEpgChannelAliasById(id string) (err error) {
	err = mapper.DeleteEpgChannelAliasById(id)
	return
}

/*
*
根据id查询用户EpgChannel
*/
func GetEpgChannelAliasById(id string) (epgChannelAlias *entity.EpgChannelAlias, err error) {
	epgChannelAlias, err = mapper.GetEpgChannelAliasById(id)
	if err != nil {
		return nil, err
	}
	return epgChannelAlias, nil
}

/*
*
更新用户信息
*/
func UpdateEpgChannelAlias(epgChannelAlias *entity.EpgChannelAlias) (err error) {
	err = mapper.UpdateEpgChannelAlias(epgChannelAlias)
	return
}
